<template>
  <div class="login login_style">
    <div class="bg" v-if="false">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title"></h3>
      <div style="margin-bottom: 20px;font-size:14px;" align="left">
        <span>只有用手机号注册的用户才能找回密码！</span>
      </div>
      <el-form-item prop="phonenumber">
        <el-input v-model="loginForm.phonenumber" type="text" auto-complete="off" placeholder="手机号码" clearable
          maxlength="11" show-word-limit
          @input="loginForm.phonenumber=loginForm.phonenumber.replace(/^(0+)|[^\d]+/g, '')"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="电 话" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <el-button size="medium" type="primary" @click="getCode" :disabled="shows">
            <span v-if="show">获取验证码</span>
            <span v-else>倒计时{{count}}s</span>
          </el-button>
        </div>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
          @keyup.enter.native="handleLogin" show-password>
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />

        </el-input>
      </el-form-item>
      <div style="margin-bottom: 20px;font-size:14px;" align="right">
        <span>已有账号？</span>
        <el-link @click="dumpUrl('login')" style="margin-bottom: 5px;color:#1890ff;">登录</el-link>
      </div>
      <el-form-item style="width:100%;">
        <!-- <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">确认修改</span>
          <span v-else>修改中</span>
        </el-button> -->
        <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="
              width: 100%;
              background: linear-gradient(54deg, #0057a1, #4087c8);
              height: 38px;
            "
            @click.native.prevent="handleLogin"
          >
            <span style="font-weight: bold;" v-if="!loading">确 认 修 改</span>
            <span v-else>登 录 中...</span>
          </el-button>
      </el-form-item>
    </el-form> 
    </div>

    <div class="login_logo">
      <div class="login_logo_img"></div>
      <div class="login_logo_txt_box">
        <span class="login_logo_txt">文嘟嘟</span>
      </div>
    </div>
    <div class="login_content">
     <div class="login_box">
      <div class="login_box_content">
          <div class="login_box_top">
            <h3>找回密码</h3>
          </div>
          <div style="margin-bottom: 20px;font-size:14px;color:#999">
            <span>只有用手机号注册的用户才能找回密码！</span>
          </div>
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="0">
            <el-form-item prop="phonenumber" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>手</span> <span>机</span><span>号</span><span>码</span></h4>
                <el-input v-model="loginForm.phonenumber" class="login_input" type="text" auto-complete="off" placeholder="请输入手机号码" clearable
                  maxlength="11" show-word-limit
                  @input="loginForm.phonenumber=loginForm.phonenumber.replace(/^(0+)|[^\d]+/g, '')"
                  @keyup.enter.native="handleLogin">
                </el-input>
              </div>
            </el-form-item>
            <el-form-item prop="code" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>验</span><span>证</span><span>码</span></h4>
                <el-input v-model="loginForm.code" class="login_input" auto-complete="off" placeholder="请输入验证码"
                  @keyup.enter.native="handleLogin"></el-input>
                <div class="login-code">
                  <el-button size="medium" type="primary" @click="getCode" :disabled="shows">
                    <span v-if="show">获取验证码</span>
                    <span v-else>倒计时{{count}}s</span>
                  </el-button>
                </div>
              </div>
            </el-form-item>
            <el-form-item prop="password" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>密</span><span>码</span></h4>
                <el-input v-model="loginForm.password" class="login_input" type="password" auto-complete="off" placeholder="请输入密码"
                  @keyup.enter.native="handleLogin" show-password>
                </el-input>
              </div>
            </el-form-item>
            <div style="margin-bottom: 20px;font-size:16px;" align="right">
              <span class="font_color">已有账号？</span>
              <el-link @click="dumpUrl('login')" type="primary" style="margin-bottom: 5px;font-size:16px;">登录</el-link>
            </div>
            <el-form-item style="width:100%;">
              <!-- <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
                @click.native.prevent="handleLogin">
                <span v-if="!loading">确认修改</span>
                <span v-else>修改中</span>
              </el-button> -->
              <el-button
                  :loading="loading"
                  type="primary"
                  style="
                    width: 100%;
                    height: 46px;
                    font-size: 18px;
                    margin-top: 35px;
                  "
                  @click.native.prevent="handleLogin"
                >
                  <span v-if="!loading">确 认 修 改</span>
                  <span v-else>登 录 中...</span>
                </el-button>
            </el-form-item>
          </el-form> 
      </div>
     </div>
    </div>

     <!--  底部  -->
      <div class="el-login-footer">
        <Footer />
      </div>
  </div>
</template>

<script>
  import {
    resetUserPwds
  } from "@/api/login";
  import Cookies from "js-cookie";
  import {
    sendCode
  } from "@/api/platform/config"
  const TIME_COUNT = 60;
  export default {
    name: "Retrieve",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else if (value.length < 6 || 16 < value.length) {
          callback(new Error('密码长度要小于等于16位并且大于等于6位'));
        } else {
          callback();
        }
      };
      var validatephone = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入手机号码'));
          this.shows = true;
        } else if (value.length !== 11) {
          callback(new Error('手机号码格式不正确'));
          this.shows = true;
        } else {
          callback();
          if (this.show) {
            this.shows = false;
          }
        }
      };
      return {
        codeUrl: "",
        cookiePassword: "",
        loginForm: {
          phonenumber: "",
          password: "",
          rememberMe: false,
          code: "",
          uuid: ""
        },

        timer: null,
        show: true,
        shows: true,
        count: '',
        loginRules: {
          phonenumber: [{
            validator: validatephone,
            trigger: "blur",
          }],
          password: [{
            validator: validatePass,
            trigger: "blur",
          }],
          code: [{
            required: true,
            trigger: "blur",
            message: "验证码不能为空"
          }]
        },
        loading: false,
        redirect: undefined
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {},
    methods: {
      getCode() {
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.shows = true;
          this.show = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              this.shows = false;
              this.show = true;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000)
          sendCode(this.loginForm.phonenumber).then(res => {
            this.msgSuccess("发送成功");
          });
        }
      },
      dumpUrl(ul) {
        let url = window.location.href;
        let arrS = url.split("retrieve");
        location.href = arrS[0] + ul;
      },
      handleLogin() {

        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            this.shows = true;
            resetUserPwds(this.loginForm).then(res => {
              this.msgSuccess("修改成功");
              Cookies.set("username", this.loginForm.phonenumber, {
                expires: 30
              });
              Cookies.remove("password");
              Cookies.remove('rememberMe');
              this.dumpUrl("login");
            }).catch(error => {
              this.loading = false;
              this.shows = false;
            })


          }
        });
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  width: 100%;
  min-height: 100%;
  background: url("../assets/images/login_bg.png") no-repeat center;
  background-size: cover;
  position: relative;
  .login_logo {
    position: absolute;
    top: 2vh;
    left: 3vw;
    display: flex;
    align-items: center;
    .login_logo_img {
      width: 41px;
      height: 40px;
      background: url("../assets/images/login_logo.png") no-repeat center;
      margin-right: 10px;
    }
    .login_logo_txt_box {
      height: 24px;
      width: 120px;
      position: relative;
      .login_logo_txt {
        position: absolute;
        top: 0;
        left: 0;
        background: linear-gradient(54deg, #0057A1, #4087C8);
        -webkit-background-clip: text;
        color: transparent;
        -webkit-text-fill-color: transparent;
        text-fill-color: transparent;
        font-size: 24px;
        line-height: 1;
        font-weight: bold;
      }
    }
  }
}
.login_content {
  height: 88vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login_box {
  width: 480px;
  height: auto;
  background-color: #FFF;
  box-shadow: 0px 0px 40px 0px rgba(27, 46, 107, 0.05);
  border-radius: 20px;
  padding: 74px 0 36px;
  .login_box_content {
    width: 334px;
    height: auto;
    margin: 0 auto;
    .login_box_top {
      display: flex;
      align-items: flex-end;
      justify-content: space-between;
      margin-bottom: 16px;
      h3 {
        color: #5DA9EF;
        font-weight: bold;
        font-size: 30px;
        line-height: 1;
      }
      .to_register {
        line-height: 1;
        font-size: 19px;
        color: #999;
      }
    }
    .form-item {
      border-bottom: 1px solid #eee;
    }
    .item_form {
      display: flex;
      align-items: center;
      .item_form_label {
        display: flex;
        justify-content: space-between;
        width: 70px;
        color: #666;
        font-size: 18px;
        line-height: 1;
        font-weight: normal;
        line-height: 50px;
      }
      .login_input {
        padding:0;
        border:none;
        outline: none;
        flex: 1;
        overflow: hidden;
        height: 50px;
        font-size: 16px;
        color: #333;
        padding: 0 16px 0 40px;
      }
    }
  }
}

.bg {
  width: 1168px;
  height: 635px;
  //background-image: url("../assets/images/Signbg.png");
  position: relative;
  margin-bottom: 80px;
}

  .title {
  width: 286px;
  height: 56px;
  //background-image: url("../assets/images/youxiao.png");
  margin: 0px auto;
  text-align: center;
}

  .login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  position: absolute;
  top: 120px;
  right: 100px;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
  height: 38px;
  display: inline-block;
  position: relative;
  top: -2px;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

  .el-login-footer {
  letter-spacing: 1px;
  margin-top: 10px;
}

.login-code-img {
  height: 38px;
}
</style>
